package com.rajesh.messageservice.helper;

import com.rajesh.messageservice.bean.Message;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class AppMsgHandler {

    private static final String ATTACH  = "6";
    private static JsonHttpClient jsonHttpClient = new JsonHttpClient();

    private static Node getChildNodeByName(NodeList appMsgNodes, String nodeName){
        for (int i = 0; i < appMsgNodes.getLength(); i++){
            Node childNode = appMsgNodes.item(i);
            if(childNode.getNodeName().equals(nodeName)){
                return childNode;
            }
        }
        return null;
    }

    public static boolean handleMsg(Message message){
        try {
            StringReader sr = new StringReader(message.getContent().replace("\n", "").replace("\t","").replace("\r",""));
            InputSource is = new InputSource(sr);
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder= factory.newDocumentBuilder();
            Document xmlDoc = builder.parse(is);

            NodeList appMsgNodes = xmlDoc.getElementsByTagName("msg").item(0)
                    .getChildNodes().item(0)
                    .getChildNodes();

            String appMsgType = getChildNodeByName(appMsgNodes, "type").getTextContent();
            switch (appMsgType){
                case ATTACH:{
                    Node appAttach = getChildNodeByName(appMsgNodes, "appattach");
                    String attachId = getChildNodeByName(appAttach.getChildNodes(), "attachid").getTextContent();
                    AppMsgMediaMap mediaMap = new AppMsgMediaMap(message.getMsgSvrId(), attachId);
                    jsonHttpClient.post(GlobalConfig.MapAppMsgUrl, mediaMap);
                    break;
                }
                default:{
                    break;
                }
            }

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}

class AppMsgMediaMap {
    private String msgSvrId;
    private String mediaSvrId;

    public void setMsgSvrId(String msgSvrId){
        this.msgSvrId = msgSvrId;
    }

    public  void setMediaSvrId(String mediaSvrId){
        this.mediaSvrId = mediaSvrId;
    }

    public String getMsgSvrId(){
        return this.msgSvrId;
    }

    public String getMediaSvrId(){
        return this.mediaSvrId;
    }

    public AppMsgMediaMap() {
    }

    public AppMsgMediaMap(String msgSvrId, String mediaSvrId) {
        this.msgSvrId = msgSvrId;
        this.mediaSvrId = mediaSvrId;
    }
}